﻿/* GZip压缩与解压类
 */

namespace LSLib.Compress
{
    using LSLib.ByteEx;
    using System;
    using System.IO;
    using System.IO.Compression;

    public static class GZipHelper
    {
        #region GZip压缩
        /// <summary>GZip压缩：Stream流
        /// </summary>
        /// <param name="sourceStream">待压缩的原数据流</param>
        /// <returns>GZip压缩过的数据流</returns>
        public static Stream GZip_Compress(Stream sourceStream)
        {
            if (sourceStream == null) throw new ArgumentNullException("sourceStream");

            MemoryStream responseStream = new MemoryStream();
            using (GZipStream gzip = new GZipStream(responseStream, CompressionMode.Compress, true))
            {
                byte[] buffer = new byte[sourceStream.Length];

                int checkCounter = sourceStream.Read(buffer, 0, buffer.Length);
                if (checkCounter != buffer.Length) throw new Exception();

                gzip.Write(buffer, 0, buffer.Length);
            }

            responseStream.Position = 0;
            return responseStream;
        }

        /// <summary>GZip压缩：byte数组
        /// </summary>
        /// <param name="sourceData">待压缩的byte数组</param>
        /// <returns>Gzip压缩过的byte数组</returns>
        public static byte[] GZip_Compress(byte[] sourceData)
        {
            if (sourceData == null) throw new ArgumentNullException("sourceData");

            using (MemoryStream sourceStream = new MemoryStream(sourceData))
            {
                Stream responseStream = GZip_Compress(sourceStream);
                return ByteHelper.ReadAllBytesFromStream(responseStream);
            }
        }
        #endregion

        #region GZip解压缩
        /// <summary>Gzip解压缩：Stream流
        /// </summary>
        /// <param name="sourceStream">待解压缩的GZip数据流</param>
        /// <returns></returns>
        public static Stream GZip_Decompress(Stream sourceStream)
        {
            if (sourceStream == null) throw new ArgumentNullException("sourceStream");

            MemoryStream responseStream = new MemoryStream();
            using (GZipStream gzip = new GZipStream(sourceStream, CompressionMode.Decompress, true))
            {
                //从压缩流中读出所有数据
                byte[] buf = new byte[1024];
                while (true)
                {
                    int one = gzip.Read(buf, 0, buf.Length);
                    if (one == 0) break;
                    responseStream.Write(buf, 0, one);
                }
            }

            responseStream.Position = 0;
            return responseStream;
        }

        /// <summary>Gzip解压缩
        /// </summary>
        /// <param name="GzipCompressedData">采用Gzip压缩过的Stream流</param>
        /// <returns>解压缩后的byte数组</returns>
        public static byte[] GZip_Decompress(byte[] GzipCompressedData)
        {
            if (GzipCompressedData == null) throw new ArgumentNullException("GzipCompressedData");

            using (MemoryStream sourceStream = new MemoryStream(GzipCompressedData))
            {
                Stream responseStream = GZip_Decompress(sourceStream);
                return ByteHelper.ReadAllBytesFromStream(responseStream);
            }
        }
        #endregion
    }
}
